package com.kingsoft.demo.excel.poi;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 读取Excel
 *
 */
public class ExcelTestA {
	
	public static void main(String[] args) {
		
		
		String filePath = "d:/1.xls";
		File file = new File(filePath);

		try {
			InputStream inputStream = new FileInputStream(file);
			Workbook workbook= new HSSFWorkbook(inputStream);
			Sheet sheet = workbook.getSheetAt(0);
			if(sheet == null) {
				System.out.println("导入错误,EXCEL中不存在Sheet工作表.");
				return;
			}
			//循环读取每行数据
			for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
				Row row = sheet.getRow(rowNum);
				if(row == null) {
					break;
				}
				int cellType;
				//读取单元格数据
				for (short cellNum = 0; cellNum < row.getLastCellNum(); cellNum++) {
					Cell cell = row.getCell(cellNum);
					String cellValue = "";
					if(cell != null) {
						cellType = cell.getCellType();
						System.out.println(cellType);
						switch (cellType) {
						case 0:  //数字
							cellValue = ""+cell.getNumericCellValue();
							break;
						case 1:  //字符串
							cellValue = cell.getStringCellValue();
							break;
							//4 -- 布尔值
						default:  
							break;
						}
						System.out.println(cellValue);
					}
					//处理每个单元格的数据(0开始)
					switch (cellNum) {
					case 0:
						//...
						break;
					case 1:
						//...
						break;
					case 2:
						//...
						break;
					case 3:
						//...
						break;
					case 4:
						//...
						break;
					default:
						break;
					}
				}
			}
			inputStream.close();
		} catch (FileNotFoundException e) {
			System.out.println("未找到文件"+ e.toString());
			e.printStackTrace();
			return ;
		} catch (IOException e) {
			System.out.println("文件无法读取"+ e.toString());
			e.printStackTrace();
			return;
		}catch (Exception e) {
			System.out.println("系统异常"+ e.toString());
			e.printStackTrace();
			return;
		}
	}

}
